<?php

class ImageConvert {

	var $image;
	var $image_type;

	function load($filename) {
		$image_info = getimagesize($filename);
		$this->image_type = $image_info[2];
		if( $this->image_type == IMAGETYPE_JPEG ) {
			$this->image = imagecreatefromjpeg($filename);
		} elseif( $this->image_type == IMAGETYPE_GIF ) {
			$this->image = imagecreatefromgif($filename);
		} elseif( $this->image_type == IMAGETYPE_PNG ) {
			$this->image = imagecreatefrompng($filename);
		}
	}
	function save($filename, $image_type=IMAGETYPE_JPEG, $compression=80, $permissions=null) {
		if( $image_type == IMAGETYPE_JPEG ) {
			imagejpeg($this->image,$filename,$compression);
		} elseif( $image_type == IMAGETYPE_GIF ) {
			imagegif($this->image,$filename);         
		} elseif( $image_type == IMAGETYPE_PNG ) {
			imagepng($this->image,$filename);
		}   
		if( $permissions != null) {
			chmod($filename,$permissions);
		}
	}
	function output($image_type=IMAGETYPE_JPEG) {
		if( $image_type == IMAGETYPE_JPEG ) {
			imagejpeg($this->image);
		} elseif( $image_type == IMAGETYPE_GIF ) {
			imagegif($this->image);         
		} elseif( $image_type == IMAGETYPE_PNG ) {
			imagepng($this->image);
		}   
	}
	
	function placeImage($path, $x, $y){
		$pic = new ImageConvert();
		$pic->load($path);
		imagecopy($this->image,$pic->image, $x, $y, 0, 0, $pic->getWidth(), $pic->getHeight());
		imagedestroy($pic->image);
		unset ($pic);
	}
	
	function getWidth() {
		return imagesx($this->image);
	}
	function getHeight() {
		return imagesy($this->image);
	}
	function resizeToHeight($height) {
		$ratio = $height / $this->getHeight();
		$width = round($this->getWidth() * $ratio);
		$this->resize($width,$height);
	}
	function resizeToWidth($width) {
		$ratio = $width / $this->getWidth();
		$height = round($this->getheight() * $ratio);
		$this->resize($width,$height);
	}
	function scale($scale) {
		$width = round($this->getWidth() * $scale/100);
		$height = round($this->getheight() * $scale/100); 
		$this->resize($width,$height);
	}
	function resize($width,$height, $crop=false) {
		$oldWidth = $this->getWidth(); 
		$oldHeight = $this->getheight(); 
		$applyWidth = $width; 
		$applyHeight = $height; 
		$startX = 0; 
		$startY = 0; 

        $new_image = imagecreatetruecolor($applyWidth, $applyHeight);
        imagefill($new_image, 0, 0, imagecolorallocate($new_image, 255, 255, 255));
        $ratioX = $width / $oldWidth; 
        $ratioY = $height / $oldHeight; 
		if ($crop){
			if ($ratioX < $ratioY) {  
			    $startX = round(($oldWidth - ($width / $ratioY))/2); 
			    $startY = 0; 
			    $oldWidth = round($width / $ratioY); 
			    $oldHeight = $oldHeight; 
			} else {  
			    $startX = 0; 
			    $startY = round(($oldHeight - ($height / $ratioX))/2); 
			    $oldWidth = $oldWidth; 
			    $oldHeight = round($height / $ratioX); 
			} 
            imagecopyresampled(
                $new_image, $this->image, 
                0, 0, 
                $startX, $startY, 
                $applyWidth, $applyHeight, 
                $oldWidth, $oldHeight
            ); 
		}
        else{
            if ($ratioX < $ratioY) {  
                $ratioY=$ratioX;
            } else {  
                $ratioX=$ratioY;
            }      
            imagecopyresampled(
                $new_image, $this->image, 
                round($applyWidth/2 - $oldWidth*$ratioX/2), round($applyHeight/2 - $oldHeight*$ratioY/2), 
                0, 0, 
                round($oldWidth*$ratioX), round($oldHeight*$ratioY), 
                $oldWidth, $oldHeight
            ); 
        }
        $this->image = $new_image;   

	}      
}


